package com.pgy.backend.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@org.aspectj.lang.annotation.Aspect
@Component
public class Aspect {
    @Pointcut("execution(public * com.pgy.backend.controller.*.*(..))")
    public void aop(){}

    @Before("aop()")
    public void before(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        System.out.println("URL : "+request.getRequestURI().toString());
        System.out.println("HTTP方法 : "+request.getMethod());
        System.out.println("IP地址 : "+request.getRemoteAddr());
        System.out.println("类的方法 : "+joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
        System.out.println("参数 : "+request.getQueryString());
    }
}
